<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  install
 */

class m_install_actions_RequirementsAction extends f_core_Action
{
    private $error = array();

	public function execute()
	{
        $this->addToModel('apacheModules', $this->checkApacheModules());
        $this->addToModel('phpConf', $this->checkPhpConf());
        $this->addToModel('phpExt', $this->checkPhpExtensions());
        $this->addToModel('error', $this->error);
		$this->setView('success');
	}	
	
	public function validate()
	{
		return true;
	}
	
	public function errorHandler()
	{
		$this->setView('error');
	}

    private function checkApacheModules()
    {
        $mods = array('mod_expires', 'mod_deflate', 'mod_headers');

        $modules = apache_get_modules();
        $result = array();

        foreach($mods as $mod)
        {
            $result[$mod] = in_array($mod, $modules);
        }

        return $result;
    }

    private function checkPhpConf()
    {
        $result = array();

        $result['magic_quotes_gpc'] = intval(ini_get('magic_quotes_gpc')) == false;
        $result['register_globals'] = intval(ini_get('register_globals')) == false;

        foreach($result as $res)
        {
            if($res == false) $this->error['phpConf'] = true;
        }

        return $result;
    }

    private function checkPhpExtensions()
    {
        $extRequired = array('pdo', 'pdo_mysql', 'gd', 'zip', 'simplexml', 'pcre', 'xmlwriter', 'hash', 'mcrypt');
        $extRecommanded = array('xdebug');

        sort($extRequired); sort($extRecommanded);

        $result = array();

        foreach($extRequired as $ext)
        {
            $isLoaded = extension_loaded($ext);

            if(!$isLoaded) $this->error['phpExt'] = true;

            $result['required'][$ext] = $isLoaded;
        }

        foreach($extRecommanded as $ext)
        {
            $result['recommanded'][$ext] = extension_loaded($ext);
        }

        return $result;
    }
}